#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "iot/thing.h"
#include "board.h"
#include "audio_codec.h"
#include <driver/gpio.h>
#include <esp_log.h>

#define TAG "UART_Control"

#define EX_UART_NUM UART_NUM_1
#define TXD_PIN 17
#define RXD_PIN 18
#define BUF_SIZE (1024)

static QueueHandle_t uart1_queue;

static void uart_rx_task(void *arg)
{
    uint8_t data;
    while (1) {
        // 检查是否有数据可读
        int len = uart_read_bytes(EX_UART_NUM, &data, 1, portMAX_DELAY);
        if (len > 0) {
            // 处理接收到的单个字节
            ESP_LOGI(TAG, "Received byte: 0x%02X", data);
            // 可根据需要添加其他处理逻辑
            uart_write_bytes(EX_UART_NUM, &data, 1);
        }
    }
}

namespace iot {

// 这里仅定义 Lamp 的属性和方法，不包含具体的实现
class UART_Control : public Thing {
private:
#ifdef CONFIG_IDF_TARGET_ESP32
    gpio_num_t gpio_num_ = GPIO_NUM_12;
#else
    gpio_num_t gpio_num_ = GPIO_NUM_21;
#endif
    bool power_ = false;

    void InitializeGpio() {
        gpio_config_t config = {
            .pin_bit_mask = (1ULL << gpio_num_),
            .mode = GPIO_MODE_OUTPUT,
            .pull_up_en = GPIO_PULLUP_DISABLE,
            .pull_down_en = GPIO_PULLDOWN_DISABLE,
            .intr_type = GPIO_INTR_DISABLE,
        };
        ESP_ERROR_CHECK(gpio_config(&config));
        gpio_set_level(gpio_num_, 0);
    }

public:
    UART_Control() : Thing("UART_Control", "一个测试用的电视"), power_(false) {
        InitializeGpio();
        /* Configure parameters of an UART driver,
        * communication pins and install the driver */
        uart_config_t uart_config = {
            .baud_rate = 115200,
            .data_bits = UART_DATA_8_BITS,
            .parity = UART_PARITY_DISABLE,
            .stop_bits = UART_STOP_BITS_1,
            .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
            .source_clk = UART_SCLK_DEFAULT,
        };
        //Install UART driver, and get the queue.
        uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart1_queue, 0);
        uart_param_config(EX_UART_NUM, &uart_config);

        //Set UART log level
        esp_log_level_set(TAG, ESP_LOG_INFO);
        //Set UART pins (using UART0 default pins ie no changes.)
        uart_set_pin(EX_UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

        // 设置接收 FIFO 阈值为 1 字节
        uart_set_rx_full_threshold(EX_UART_NUM, 1);
        // 启用接收中断
        uart_enable_rx_intr(EX_UART_NUM);

        // 定义设备的属性
        properties_.AddBooleanProperty("power", "电视是否打开", [this]() -> bool {
            return power_;
        });

        // 定义设备可以被远程执行的指令
        methods_.AddMethod("TurnOn", "打开电视", ParameterList(), [this](const ParameterList& parameters) {
            power_ = true;
            gpio_set_level(gpio_num_, 1);
            const char* message = "TurnOn";
            uart_write_bytes(EX_UART_NUM, (const uint8_t*)message, strlen(message));
        });

        methods_.AddMethod("TurnOff", "关闭电视", ParameterList(), [this](const ParameterList& parameters) {
            power_ = false;
            gpio_set_level(gpio_num_, 0);
            const char* message = "TurnOff";
            uart_write_bytes(EX_UART_NUM, (const uint8_t*)message, strlen(message));
        });
    }
};

} // namespace iot

DECLARE_THING(UART_Control);
